<?php
namespace Users\Controller;
use Hybrid_Auth;
use ScnSocialAuth\Mapper\Exception as MapperException;
use ScnSocialAuth\Mapper\UserProviderInterface;
use ScnSocialAuth\Options\ModuleOptions;
use Zend\View\Model\ModelInterface;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Users\Model\User;
use Users\Model\Usernew;
use Application\Model\Cities;          // <-- Add this import
use Users\Form\UserForm; 
use Zend\View\Model\JsonModel;
use Zend\Session\Container;
use Users\Model\Forgetpassword;
use Application\Model\country;
use Application\Model\state;
use Users\Model\Flavor;
use Products\Model\Productsnew;
use Products\Model\Bid;
class UsersController extends AbstractActionController
{
	protected  $userTable;
	protected  $forgetpasswordTable;
	protected  $countryTable;
	protected  $stateTable;
	protected  $usersnewTable;
	protected  $productsnewTable;
	protected  $bidTable;
	protected  $adminCombinationsTable;

	protected  $userRegionsTable;
	
	  /**
     * @var UserProviderInterface
     */
    protected $mapper;

    /**
     * @var Hybrid_Auth
     */
    protected $hybridAuth;

    /**
     * @var ModuleOptions
     */
    protected $options;

    /*
     * @todo Make this dynamic / translation-friendly
     * @var string
     */
    protected $failedAddProviderMessage = 'Add provider failed. Please try again.';
	public function setOptions(ModuleOptions $options)
    {
        $this->options = $options;

        return $this;
    }
	public function staticAction()
	{
    $pageName = $this->params('page_name');
	
    $view = new ViewModel();
    $view->setTemplate('static/' . $pageName); 

    return $view;
	}
    public function indexAction()
	{
		
	}
	
    public function addAdminAction()
	{
        if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']=="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			$user_session->indsCntrysCount=$_POST['indsCntrysCount'];
			$user_session->indsCntrysIds=$_POST['indsCntrysIds'];
			$user_session->industries=$_POST['industry'];
			$user_session->countries=$_POST['country'];
			$industry=explode("/",$_SESSION['usersinfo']['industries'][0]);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];
			$country=explode("-",$_SESSION['usersinfo']['countries'][0]);
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			$user_session->adminCombinations=array_map( array( $this,'myArraysCombine' ),$_SESSION['usersinfo']['industries'],$_SESSION['usersinfo']['countries'] );
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('user-next');
			}			
	    }else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			$user_session->indsCntrysCount=$_POST['indsCntrysCount'];
			$user_session->indsCntrysIds=$_POST['indsCntrysIds'];
			$user_session->industries=$_POST['industry'];
			$user_session->countries=$_POST['country'];
			$industry=explode("/",$_SESSION['usersinfo']['industries'][0]);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];			
			$country=explode("-",$_SESSION['usersinfo']['countries'][0]);
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			$user_edit_session = new Container('edituser');
			// echo "<pre>";print_r($_SESSION['usersinfo']['countries']);print_r($_SESSION['usersinfo']['industries']);exit;
			$user_edit_session->adminCombinations=array_map( array( $this,'myArraysCombine' ),$_SESSION['usersinfo']['industries'],$_SESSION['usersinfo']['countries'] );
			// echo "<pre>";print_r($_SESSION['edituser']['adminCombinations']);exit;
			if($_POST['hid_user_id']!=""){
				return $this->redirect()->toUrl('user-next?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
	    }else if(isset($_GET['edit']) && $_GET['edit']!=""){
			 //unset($_SESSION['usersinfo']);
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->getUser($_GET['user_id']);
			 $loadedCombinations = "";
			 $existingCombsCount = 0;
			 if( isset($_SESSION['edituser']['adminCombinations']) )
			 {
			 
				$existingCombsCount = count( $_SESSION['edituser']['adminCombinations'] );
				 
				 for( $indCtryId = 0; $indCtryId < $existingCombsCount; $indCtryId++ )
				 {
					foreach( $_SESSION['edituser']['adminCombinations'][$indCtryId] as $industryInfo=>$countryInfo )
					{
						$loadedCombinations .= $industryInfo . "#" . $countryInfo . "!";
					}
				 }
				$loadedCombinations = substr( $loadedCombinations,0,(strlen($loadedCombinations)-1) );
				// echo $loadedCombinations;exit;
			 }
			 else
			 {
				 $existingCombsCount = $this->getAdminCombinationsTable()->getCombsCount( $_GET['user_id'] );
				 $loadedCombinations = $this->getAdminCombinationsTable()->loadCombinations( $_GET['user_id'] );
			 }
			 
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 $industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			 if($getdetails->parent_user_id!=""){
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$parnetgetdetails = $table->editUser($getdetails->parent_user_id);
				$parent_user_type=$parnetgetdetails->user_type;	
				if($parent_user_type=='superadmin'){
					$getallindustries=$industrytable->getallindustries();
				}else{
					$getallindustries=$industrytable->getindustries($parnetgetdetails->industry);
				}
			 }						 

			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
				'getallindustries' 	=> $getallindustries,
				'existingCombsCount' 	=> $existingCombsCount,
				'loadedCombinations' 	=> $loadedCombinations,
			 ));
	    }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			unset($_SESSION['usersinfo']);	
			unset($_SESSION['edituser']);
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->getUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 $industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			 if($getdetails->user_type=='superadmin'){
				$getallindustries=$industrytable->getallindustries();
			}else{
				$getallindustries=$industrytable->getindustries($getdetails->industry);
			}			 
			 $existingCombsCount = $this->getAdminCombinationsTable()->getCombsCount( $_GET['user_id'] );
			 $loadedCombinations = $this->getAdminCombinationsTable()->loadCombinations( $_GET['user_id'] );
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
				'getallindustries' 	=> $getallindustries,
				'existingCombsCount' 	=> $existingCombsCount,
				'loadedCombinations' 	=> $loadedCombinations,
			 ));
	    }else if(isset($_GET['is_first']) && $_GET['is_first']=="1"){
				unset($_SESSION['usersinfo']);
				unset($_SESSION['edituser']);
				$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				$getCountries=$countrytable->getallcountries();
				$getallindustries='';
				return new ViewModel(array(
					'countries' 	=> $getCountries,
					'getallindustries' 	=> $getallindustries,
				));
		}else{
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();
			$getallindustries='';
				return new ViewModel(array(
					'countries' 	=> $getCountries,
					'getallindustries' 	=> $getallindustries,
				));
		}
	}
	
	public function userNextAction(){
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=1;
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('user-confirm');
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){			
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=$_POST['status'];	
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){
				return $this->redirect()->toUrl('user-confirm?user_id='.$_POST['hid_user_id']);
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){
				return $this->redirect()->toUrl('user-confirm?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
		}else if(isset($_GET['edit']) && $_GET['edit']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
				//unset($_SESSION['usersinfo']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
		}else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
				//unset($_SESSION['usersinfo']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
		}else{		
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			return new ViewModel(array(
				'countries' 	=> $getCountries,
			 ));
		}		
	}	

	public function userConfirmAction(){//echo '<pre>'; print_r($_POST);exit;
		include('public/PHPMailer_5.2.4/sendmail.php');
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){			
			$password = $this->getUniqueCode('10');	
			if($_POST['hid_user_id']==""){
				$get_user_id = $this->getusersnewTable()->adduser($_POST,$password);
				$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $get_user_id->user_id,$_SESSION['usersinfo']['adminCombinations'] );
				$username=ucfirst($get_user_id->company_name);
				$createEmailid=$get_user_id->email;
				$createusertype=$get_user_id->user_type;
				if($get_user_id->parent_user_id!=""){
					$get_parent_details = $this->getusersnewTable()->editUser($get_user_id->parent_user_id);
					//parent details
						$parentEmail=$get_parent_details->email;
						$parentName=ucfirst($get_parent_details->company_name);
						$parentUsertype=$get_parent_details->user_type;
					if($get_parent_details->parent_user_id!=""){						
						$get_super_details = $this->getusersnewTable()->editUser($get_parent_details->parent_user_id);
						if($get_super_details!=""){
							global $regSubject;				
							global $regMessage;
							global $sentParentSubject;
							global $sentParentMessage;
							global $sentsuperadminSubject;
							global $sentsuperadminMessage;
							//superadmin details
							$superadminEmail=$get_super_details->email;
							$superadminName=ucfirst($get_super_details->company_name);
							
							$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
							$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
							$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
							
							$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
							
							$sentsuperadminMessage = str_replace("<FULLNAME>","$superadminName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<PARENTNAME>","$parentName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<CREATEDUSERNAME>","$username", $sentsuperadminMessage);
							
							unset($_SESSION['usersinfo']);
							if(sendMail($createEmailid,$regSubject,$regMessage)){
								if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
									if(sendMail($superadminEmail,$sentsuperadminSubject,$sentsuperadminMessage)){
										if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
											return $this->redirect()->toUrl('admin_allusers-list');
										}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
											return $this->redirect()->toUrl('allusers-list');
										}
									}
								}
							}
						}
					}else{
						global $regSubject;				
						global $regMessage;
						global $sentParentSubject;
						global $sentParentMessage;	
						
						$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
						$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
						$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
						
						$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
						
						unset($_SESSION['usersinfo']);
						if(sendMail($createEmailid,$regSubject,$regMessage)){
							if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
								if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
									return $this->redirect()->toUrl('admin_allusers-list');
								}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
									return $this->redirect()->toUrl('allusers-list');
								}
							}
						}
					}
				}else{
					global $regSubject;				
					global $regMessage;					
					$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
					$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
					$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
					unset($_SESSION['usersinfo']);
					if(sendMail($createEmailid,$regSubject,$regMessage)){
						if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
							return $this->redirect()->toUrl('allusers-list');
						}
					}
				}
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){				
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){	
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['usersinfo']['adminCombinations'] );
				}
				else if( isset($_SESSION['edituser']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['edituser']['adminCombinations'] );
				}
				unset($_SESSION['userinfo']);
				if($get_user_id>=0){					
						return $this->redirect()->toUrl('allusers-list');
				}
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){				
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['usersinfo']['adminCombinations'] );
				}
				else if( isset($_SESSION['edituser']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['edituser']['adminCombinations'] );
				}
				unset($_SESSION['userinfo']);
				if($get_user_id>=0){					
					if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else{
							return $this->redirect()->toUrl('allusers-list');
						}
					}else if(($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('allusers-list');
						}else{
							return $this->redirect()->toUrl('allusers-list');
						}
					}else{
							return $this->redirect()->toUrl('allusers-list');
					}
				}
			}
	  }else if(isset($_GET['edit']) && $_GET['edit']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
				//unset($_SESSION['usersinfo']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
	  }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
				//unset($_SESSION['usersinfo']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
	  }else{		
		$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
		 $getCountries=$countrytable->getallcountries();
		return new ViewModel(array(
			'countries' 	=> $getCountries,
		 ));
	  }
	}

    public function addUserAction()
	{
        if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']=="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){
				$admin_adding_user_id=explode("/",$_POST['admin_users']);
				$user_session->adminuser_user_id=$admin_adding_user_id[0];
				$user_session->adminuser_user_name=$admin_adding_user_id[1];
			}else{
				$user_session->adminuser_user_id='';
				$user_session->adminuser_user_name='';
			}
			$user_session->indsCntrysCount=$_POST['indsCntrysCount'];
			$user_session->regionsInfo=$_POST['indsCntrysIds'];
			$user_session->regions=$_POST['region'];
			$industry=explode("/",$_POST['industry']);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];
			$country=explode("/",$_POST['country']);
			if( ! array_key_exists( 1,$country) )
			{
				$country=explode("-",$_POST['country']);
			}
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('adduser-next');
			}			
	    }else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin'){
				$admin_adding_user_id=explode("/",$_POST['admin_users']);
				$user_session->adminuser_user_id=$admin_adding_user_id[0];
				$user_session->adminuser_user_name=$admin_adding_user_id[1];
			}else{
				$user_session->adminuser_user_id=$_SESSION['user']['user_id'];
				$user_session->adminuser_user_name=$_SESSION['user']['username'];
			}
			$user_session->indsCntrysCount=$_POST['indsCntrysCount'];
			$user_session->regionsInfo=$_POST['indsCntrysIds'];
			$user_session->regions=$_POST['region'];
			$industry=explode("/",$_POST['industry']);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];			
			$country=explode("/",$_POST['country']);
			if( ! array_key_exists( 1,$country) )
			{
				$country=explode("-",$_POST['country']);
			}
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=='superadmin')
			{
				$user_edit_admin_session = new Container('editadmin');
				$user_edit_admin_session->updatedAdminInfo=$_POST['admin_users'];
			}
			$user_edit_regions_session = new Container('editregions');
			$user_edit_regions_session->regionsInfo=$_POST['indsCntrysIds'];
			if($_POST['hid_user_id']!=""){
				return $this->redirect()->toUrl('adduser-next?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
	    }else if(isset($_GET['edit']) && $_GET['edit']!=""){
			 // unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->getUser($_GET['user_id']);
			 
			$table1 = $this->getServiceLocator()->get('Application\Model\CountryTable');
			$countryRow = $table1->getcountry( $getdetails->country );
			$countryName = $countryRow->nicename;
			
			$sessCntrysId = $getdetails->country . "/" . $countryName;
			 
			 $sessAdminInfo = "";
			 if( isset($_SESSION['editadmin']['updatedAdminInfo']) )
			 {
				$sessAdminInfo = $_SESSION['editadmin']['updatedAdminInfo'];
			 }
			 
			 $loadedRegions = "";
			 $existingRegionsCount = 0;
			 if( isset($_SESSION['editregions']['regionsInfo']) )
			 {
				$existingRegionsArray = explode( "!",$_SESSION['editregions']['regionsInfo'] );
				$existingRegionsCount = count( $existingRegionsArray );
				$loadedRegions = $_SESSION['editregions']['regionsInfo'];
			 }
			 else
			 {
				 $existingRegionsCount = $this->getUserRegionsTable()->getRegionsCount( $_GET['user_id'] );
				 $loadedRegions = $this->getUserRegionsTable()->loadRegions( $_GET['user_id'] );
			 }
			 
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');			 
			 $industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			 if($getdetails->parent_user_id!=""){
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$parnetgetdetails = $table->editUser($getdetails->parent_user_id);
				$parent_user_type=$parnetgetdetails->user_type;	
				$getallindustries=$this->getAdminCombinationsTable()->getindustries($getdetails->parent_user_id,'industry');
				$getRegions=$this->getAdminCombinationsTable()->getindustries($getdetails->parent_user_id,'country');
				$getCountries=$countrytable->getallcountries();					
			}
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getalladmins = $table->getalladmins();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
				'sessAdminInfo' 	=> $sessAdminInfo,
				'sessCntrysId' 	=> $sessCntrysId,
				'getRegions' 	=> $getRegions,
				'getallindustries' 	=> $getallindustries,
				'existingRegionsCount' 	=> $existingRegionsCount,
				'loadedRegions' 	=> $loadedRegions,
				'getalladmins'  => $getalladmins,
			 ));
	    }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			unset($_SESSION['usersinfo']);
			unset($_SESSION['editadmin']);
			unset($_SESSION['editregions']);
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getdetails = $table->getUser($_GET['user_id']);

			$table1 = $this->getServiceLocator()->get('Application\Model\CountryTable');
			$countryRow = $table1->getcountry( $getdetails->country );
			$countryName = $countryRow->nicename;
			
			$sessCntrysId = $getdetails->country . "/" . $countryName;

			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');			
			$industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');	
			$getallindustries=$this->getAdminCombinationsTable()->getindustries($getdetails->parent_user_id,'industry');
			$getRegions=$this->getAdminCombinationsTable()->getindustries($getdetails->parent_user_id,'country');
			$getCountries=$countrytable->getallcountries();			
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getalladmins = $table->getalladmins();
			$existingRegionsCount = $this->getUserRegionsTable()->getRegionsCount( $_GET['user_id'] );
			$loadedRegions = $this->getUserRegionsTable()->loadRegions( $_GET['user_id'] );
			
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'getRegions' 	=> $getRegions,
				'countries' 	=> $getCountries,
				'sessCntrysId' 	=> $sessCntrysId,
				'getallindustries' 	=> $getallindustries,
				'getalladmins' 	=> $getalladmins,
				'existingRegionsCount' 	=> $existingRegionsCount,
				'loadedRegions' 	=> $loadedRegions,
			 ));
	    }else if(isset($_GET['is_first']) && $_GET['is_first']=="1"){
				unset($_SESSION['usersinfo']);
				unset($_SESSION['editadmin']);
				unset($_SESSION['editregions']);
				$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');				
				$getallindustries=$this->getAdminCombinationsTable()->getindustries($_SESSION['user']['user_id'],'industry');
				$getRegions=$this->getAdminCombinationsTable()->getindustries($_SESSION['user']['user_id'],'country');	
				$getCountries=$countrytable->getallcountries();		
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getalladmins = $table->getalladmins();
				return new ViewModel(array(
					'getRegions' 	=> $getRegions,
				     'countries' 	=> $getCountries,
					'getallindustries' 	=> $getallindustries,
					'getalladmins' 	=> $getalladmins,
				));
		}else{
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			$getallindustries=$this->getAdminCombinationsTable()->getindustries($_SESSION['user']['user_id'],'industry');
			$getRegions=$this->getAdminCombinationsTable()->getindustries($_SESSION['user']['user_id'],'country');	
			$getCountries=$countrytable->getallcountries();	
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getalladmins = $table->getalladmins();
			return new ViewModel(array(
				'countries' 	=> $getCountries,
				'getRegions' 	=> $getRegions,
				'getallindustries' 	=> $getallindustries,
				'getalladmins' 	=> $getalladmins,
			));
		}
	}

	public function addUserNextAction(){
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=1;
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('adduser-confirm');
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){			
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=$_POST['status'];	
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){
				return $this->redirect()->toUrl('adduser-confirm?user_id='.$_POST['hid_user_id']);
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){
				return $this->redirect()->toUrl('adduser-confirm?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
		}else if(isset($_GET['edit']) && $_GET['edit']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
			 // unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		 }
		}else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
				// unset($_SESSION['usersinfo']);
				 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				 $getdetails = $table->editUser($_GET['user_id']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
		}else{		
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			return new ViewModel(array(
				'countries' 	=> $getCountries,
			 ));
		}		
	}
	
	public function addUserConfirmAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){			
			$password = $this->getUniqueCode('10');	
			if($_POST['hid_user_id']==""){
				$get_user_id = $this->getusersnewTable()->adduser($_POST,$password);				
				$add_regions_id = $this->getUserRegionsTable()->addRegions( $get_user_id->user_id,$_SESSION['usersinfo']['regionsInfo'] );
				if($add_regions_id>0){
				$username=ucfirst($get_user_id->company_name);
				$createEmailid=$get_user_id->email;
				$createusertype=$get_user_id->user_type;
				if($get_user_id->parent_user_id!=""){
					$get_parent_details = $this->getusersnewTable()->editUser($get_user_id->parent_user_id);
					//parent details
						$parentEmail=$get_parent_details->email;
						$parentName=ucfirst($get_parent_details->company_name);
						$parentUsertype=$get_parent_details->user_type;
					if($get_parent_details->parent_user_id!=""){						
						$get_super_details = $this->getusersnewTable()->editUser($get_parent_details->parent_user_id);
						if($get_super_details!=""){
							global $regSubject;				
							global $regMessage;
							global $sentParentSubject;
							global $sentParentMessage;
							global $sentsuperadminSubject;
							global $sentsuperadminMessage;
							//superadmin details
							$superadminEmail=$get_super_details->email;
							$superadminName=ucfirst($get_super_details->company_name);
							
							$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
							$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
							$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
							
							$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
							
							$sentsuperadminMessage = str_replace("<FULLNAME>","$superadminName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<PARENTNAME>","$parentName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<CREATEDUSERNAME>","$username", $sentsuperadminMessage);
							
							unset($_SESSION['usersinfo']);
							if(sendMail($createEmailid,$regSubject,$regMessage)){
								if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
									if(sendMail($superadminEmail,$sentsuperadminSubject,$sentsuperadminMessage)){
										if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
											return $this->redirect()->toUrl('admin_allusers-list');
										}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
											return $this->redirect()->toUrl('allusers-list');
										}
									}
								}
							}
						}
					}else{
						global $regSubject;				
						global $regMessage;
						global $sentParentSubject;
						global $sentParentMessage;	
						
						$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
						$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
						$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
						
						$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
						
						unset($_SESSION['usersinfo']);
						if(sendMail($createEmailid,$regSubject,$regMessage)){
							if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
								if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
									return $this->redirect()->toUrl('admin_allusers-list');
								}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
									return $this->redirect()->toUrl('allusers-list');
								}
							}
						}
					}
				}else{
					global $regSubject;				
					global $regMessage;					
					$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
					$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
					$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
					unset($_SESSION['usersinfo']);
					if(sendMail($createEmailid,$regSubject,$regMessage)){
						if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
							return $this->redirect()->toUrl('allusers-list');
						}
					}
				}
				}
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){						
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){	
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['regionsInfo']) )
				{
					$add_regions_id = $this->getUserRegionsTable()->addRegions( $_POST['hid_user_id'],$_SESSION['usersinfo']['regionsInfo'] );
				}
				else if( isset($_SESSION['editregions']['regionsInfo']) )
				{
					$add_regions_id = $this->getUserRegionsTable()->addRegions( $_POST['hid_user_id'],$_SESSION['editregions']['regionsInfo'] );
				}
				unset($_SESSION['userinfo']);
				unset($_SESSION['editregions']);
				unset($_SESSION['editadmin']);
				if($get_user_id>=0){					
					return $this->redirect()->toUrl('adduser?user_id='.$_POST['hid_user_id']);
				}
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){				
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['regionsInfo']) )
				{
					$add_regions_id = $this->getUserRegionsTable()->addRegions( $_POST['hid_user_id'],$_SESSION['usersinfo']['regionsInfo'] );
				}
				else if( isset($_SESSION['editregions']['regionsInfo']) )
				{
					$add_regions_id = $this->getUserRegionsTable()->addRegions( $_POST['hid_user_id'],$_SESSION['editregions']['regionsInfo'] );
				}
				unset($_SESSION['userinfo']);
				unset($_SESSION['editregions']);
				unset($_SESSION['editadmin']);
				if($get_user_id>=0){					
					if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else{
							return $this->redirect()->toUrl('adduser?user_id='.$_POST['hid_user_id']);
						}
					}else if(($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('allusers-list');
						}else{
							return $this->redirect()->toUrl('adduser?user_id='.$_POST['hid_user_id']);
						}
					}else{
						return $this->redirect()->toUrl('adduser?user_id='.$_POST['hid_user_id']);
					}
				}
			}
	  }else if(isset($_GET['edit']) && $_GET['edit']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
			 // unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		}
	  }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$getdetails = $this->getusersnewTable()->getUser( $_GET['user_id'] );
			}else{
				$getdetails='';
			}
			if($getdetails!=''){
			// unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		}
	  }else{		
		$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
		 $getCountries=$countrytable->getallcountries();
		return new ViewModel(array(
			'countries' 	=> $getCountries,
		 ));
	  }
	}

	public function gotoBackAction(){
		$getdetails_superadmin = $this->getusersnewTable()->getdetails_superadmin('superadmin');
		if($getdetails_superadmin!=""){
			$user_details = $this->getusersnewTable()->editUser($getdetails_superadmin->user_id);
			$array =  (array) $user_details;
			$checkLogin = $this->getusersnewTable()->checkLogin($array,$authreg='check');
			if($checkLogin!=''){					
				$user_session = new Container('user');
				$user_session->username=$checkLogin->company_name;
				$user_session->email=$checkLogin->email;
				$user_session->user_id=$checkLogin->user_id;
				$user_session->user_type=$checkLogin->user_type;
				$user_session->countryname=$checkLogin->usercountry;
				$user_session->country_id=$checkLogin->country;
				$user_session->cityname=$checkLogin->city;
				$user_session->gotoBack='gotoBack';
				if($checkLogin->user_id>0){
					$countProducts = $this->getProductsnewTable()->getActivestatus($checkLogin->user_id);
					$user_session->countProducts=$countProducts;
					$countBidrecived = $this->getBidTable()->bidrecivedCount($checkLogin->user_id);
					$user_session->countBidrecived=$countBidrecived;
				}
				return $this->redirect()->toUrl('superadmin-first?hello_first=hello');	
			}	
		}		
	}
	public function superadminFirstAction(){
	$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basepath = $baseUrlArr['basePath'];
	if(isset($_GET['user_id']) && $_GET['user_id']!=""){
		$user_details = $this->getusersnewTable()->editUser($_GET['user_id']);
		$array =  (array) $user_details;
		$checkLogin = $this->getusersnewTable()->checkLogin($array,$authreg='check');
		if($checkLogin!=''){					
			$user_session = new Container('user');
			$user_session->username=$checkLogin->company_name;
			$user_session->email=$checkLogin->email;
			$user_session->user_id=$checkLogin->user_id;
			$user_session->user_type=$checkLogin->user_type;
			$user_session->countryname=$checkLogin->usercountry;
			$user_session->country_id=$checkLogin->country;
			$user_session->cityname=$checkLogin->city;
			$user_session->gotoBack='gotoBack';
			if($checkLogin->user_id>0){
				$countProducts = $this->getProductsnewTable()->getActivestatus($checkLogin->user_id);
				$user_session->countProducts=$countProducts;
				$countBidrecived = $this->getBidTable()->bidrecivedCount($checkLogin->user_id);
				$user_session->countBidrecived=$countBidrecived;
			}
			if($checkLogin->user_type=='admin'){			
				return $this->redirect()->toRoute('adminprofile');	
			}else if($checkLogin->user_type=='superadmin'){
				return $this->redirect()->toUrl('user-previous?user_id='.$checkLogin->user_id);
			}
		}			
	}else{
		$getalladmins = $this->getusersnewTable()->getalladmins();
		$getdetails_superadmin = $this->getusersnewTable()->getdetails_superadmin('superadmin');
		return new ViewModel(array(
			'getalladmins' 	=> $getalladmins,
			'getdetails_superadmin' 	=> $getdetails_superadmin,
		 ));
		}
	}

	public function userPreviousAction(){ 
        if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']=="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			$industry=explode("/",$_POST['industry']);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];
			$country=explode("-",$_POST['country']);
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('super-user-next');
			}			
	    }else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cname']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cname=$_POST['cname'];
			$industry=explode("/",$_POST['industry']);
			$user_session->industry=$industry[0];
			$user_session->industry_name=$industry[1];
			$user_session->address1=$_POST['address1'];
			$user_session->address2=$_POST['address2'];
			$user_session->address3=$_POST['address3'];
			$user_session->lmnear=$_POST['lmnear'];
			$user_session->city=$_POST['city'];			
			$country=explode("-",$_POST['country']);
			$user_session->country=$country[0];
			$user_session->country_name=$country[1];
			$user_session->email=$_POST['email'];
			$user_session->phone=$_POST['phone'];
			$user_session->fax=$_POST['fax'];
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){
				return $this->redirect()->toUrl('super-user-next?user_id='.$_POST['hid_user_id']);
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){
				return $this->redirect()->toUrl('super-user-next?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
	    }else if(isset($_GET['edit']) && $_GET['edit']!=""){			
			 unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 $industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			 if($getdetails->parent_user_id!=""){
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$parnetgetdetails = $table->editUser($getdetails->parent_user_id);
				$parent_user_type=$parnetgetdetails->user_type;	
				if($parent_user_type=='superadmin'){
					$getallindustries=$industrytable->getallindustries();
				}else{
					$getallindustries=$industrytable->getindustries($parnetgetdetails->industry);
				}
			 }						 
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
				'getallindustries' 	=> $getallindustries,
			 ));
	    }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 $industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			 if($getdetails->user_type=='superadmin'){
				$getallindustries=$industrytable->getallindustries();
			}else{
				$getallindustries=$industrytable->getindustries($getdetails->industry);
			}			 
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
				'getallindustries' 	=> $getallindustries,
			 ));
	    }else if(isset($_GET['is_first']) && $_GET['is_first']=="1"){
				unset($_SESSION['usersinfo']);
				$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				$getdetails = $table->editUser($_SESSION['user']['user_id']);
				$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				$getCountries=$countrytable->getallcountries();
				$industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
				if($getdetails->user_type=='superadmin'){
					$getallindustries=$industrytable->getallindustries();
				}else{
					$getallindustries=$industrytable->getindustries($getdetails->industry);
				}	
				return new ViewModel(array(
					'countries' 	=> $getCountries,
					'getallindustries' 	=> $getallindustries,
				));
		}else{
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();
			$industrytable = $this->getServiceLocator()->get('Products\Model\IndustryTable');
			$getallindustries=$industrytable->getallindustries();
			return new ViewModel(array(
				'countries' 	=> $getCountries,
				'getallindustries' 	=> $getallindustries,
			));
		}
	}

	public function superUserNextAction(){
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=1;
			if($_POST['hid_user_id']==""){
				return $this->redirect()->toUrl('super-user-confirm');
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){			
			$user_session = new Container('usersinfo');
			$user_session->cperson=$_POST['cperson'];
			$user_session->mobile=$_POST['mobile'];
			$user_session->second_email=$_POST['email'];
			$user_session->baddress=$_POST['baddress'];
			$user_session->acno=$_POST['acno'];
			$user_session->note=$_POST['note'];
			$user_session->type=$_POST['type'];	
			$user_session->status=$_POST['status'];	
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){
				return $this->redirect()->toUrl('super-user-confirm?user_id='.$_POST['hid_user_id']);
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){
				return $this->redirect()->toUrl('super-user-confirm?user_id='.$_POST['hid_user_id'].'&edit=1');
			}
		}else if(isset($_GET['edit']) && $_GET['edit']!=""){
		if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
			$get_user_id = $this->getusersnewTable()->editupdate($_SESSION['usersinfo'],$_GET['user_id']);
		}else{
			$get_user_id='0';
		}
		 if($get_user_id>=0){
			 unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		 }
		}else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
				$get_user_id = $this->getusersnewTable()->editupdate($_SESSION['usersinfo'],$_GET['user_id']);
			}else{
				$get_user_id='0';
			}
			if($get_user_id>=0){
				unset($_SESSION['usersinfo']);
				 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
				 $getdetails = $table->editUser($_GET['user_id']);
				 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
				 $getCountries=$countrytable->getallcountries();
				 return new ViewModel(array(
					'user_details' 	=> $getdetails,
					'countries' 	=> $getCountries,
				 ));
			 }
		}else{		
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			return new ViewModel(array(
				'countries' 	=> $getCountries,
			 ));
		}		
	}	

	public function superUserConfirmAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']==""&& $_POST['cperson']!=""){			
			$password = $this->getUniqueCode('10');	
			if($_POST['hid_user_id']==""){
				$get_user_id = $this->getusersnewTable()->adduser($_POST,$password);
				$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $get_user_id->user_id,$_SESSION['usersinfo']['adminCombinations'] );
				$username=ucfirst($get_user_id->company_name);
				$createEmailid=$get_user_id->email;
				$createusertype=$get_user_id->user_type;
				if($get_user_id->parent_user_id!=""){
					$get_parent_details = $this->getusersnewTable()->editUser($get_user_id->parent_user_id);
					//parent details
						$parentEmail=$get_parent_details->email;
						$parentName=ucfirst($get_parent_details->company_name);
						$parentUsertype=$get_parent_details->user_type;
					if($get_parent_details->parent_user_id!=""){						
						$get_super_details = $this->getusersnewTable()->editUser($get_parent_details->parent_user_id);
						if($get_super_details!=""){
							global $regSubject;				
							global $regMessage;
							global $sentParentSubject;
							global $sentParentMessage;
							global $sentsuperadminSubject;
							global $sentsuperadminMessage;
							//superadmin details
							$superadminEmail=$get_super_details->email;
							$superadminName=ucfirst($get_super_details->company_name);
							
							$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
							$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
							$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
							
							$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
							$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
							
							$sentsuperadminMessage = str_replace("<FULLNAME>","$superadminName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<PARENTNAME>","$parentName", $sentsuperadminMessage);						
							$sentsuperadminMessage = str_replace("<CREATEDUSERNAME>","$username", $sentsuperadminMessage);
							
							unset($_SESSION['usersinfo']);
							if(sendMail($createEmailid,$regSubject,$regMessage)){
								if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
									if(sendMail($superadminEmail,$sentsuperadminSubject,$sentsuperadminMessage)){
										if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
											return $this->redirect()->toUrl('admin_allusers-list');
										}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
											return $this->redirect()->toUrl('allusers-list');
										}
									}
								}
							}
						}
					}else{
						global $regSubject;				
						global $regMessage;
						global $sentParentSubject;
						global $sentParentMessage;	
						
						$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
						$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
						$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
						
						$sentParentMessage = str_replace("<FULLNAME>","$parentName", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERNAME>","$username", $sentParentMessage);						
						$sentParentMessage = str_replace("<CREATEDUSERTYPE>","$createusertype", $sentParentMessage);
						
						unset($_SESSION['usersinfo']);
						if(sendMail($createEmailid,$regSubject,$regMessage)){
							if(sendMail($parentEmail,$sentParentSubject,$sentParentMessage)){
								if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
									return $this->redirect()->toUrl('admin_allusers-list');
								}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
									return $this->redirect()->toUrl('allusers-list');
								}
							}
						}
					}
				}else{
					global $regSubject;				
					global $regMessage;					
					$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
					$regMessage = str_replace("<EMAIL>","$createEmailid", $regMessage);
					$regMessage = str_replace("<PASSWORD>","$password", $regMessage);
					unset($_SESSION['usersinfo']);
					if(sendMail($createEmailid,$regSubject,$regMessage)){
						if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
							return $this->redirect()->toUrl('allusers-list');
						}
					}
				}
			}
		}else if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!="" && $_POST['cperson']!=""){						
			if($_POST['hid_user_id']!="" && $_POST['edit_status']==''){	
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['usersinfo']['adminCombinations'] );
				}
				else if( isset($_SESSION['edituser']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['edituser']['adminCombinations'] );
				}
				unset($_SESSION['userinfo']);
				if($get_user_id>=0){					
					return $this->redirect()->toUrl('user-previous?user_id='.$_POST['hid_user_id']);
				}
			}else if($_POST['hid_user_id']!="" && $_POST['edit_status']!=''){				
				$get_user_id = $this->getusersnewTable()->updateUser($_POST,$_POST['hid_user_id']);
				if( isset($_SESSION['usersinfo']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['usersinfo']['adminCombinations'] );
				}
				else if( isset($_SESSION['edituser']['adminCombinations']) )
				{
					$add_combinations_id = $this->getAdminCombinationsTable()->addCombinations( $_POST['hid_user_id'],$_SESSION['edituser']['adminCombinations'] );
				}
				unset($_SESSION['userinfo']);
				if($get_user_id>=0){					
					if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('admin_allusers-list');
						}else{
							return $this->redirect()->toUrl('user-previous?user_id='.$_POST['hid_user_id']);
						}
					}else if(($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){
						if($_POST['edit_status']!=""){
							return $this->redirect()->toUrl('allusers-list');
						}else{
							return $this->redirect()->toUrl('user-previous?user_id='.$_POST['hid_user_id']);
						}
					}else{
						return $this->redirect()->toUrl('user-previous?user_id='.$_POST['hid_user_id']);
					}
				}
			}
	  }else if(isset($_GET['edit']) && $_GET['edit']!=""){
		 if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
			$get_user_id = $this->getusersnewTable()->editUpdate2($_SESSION['usersinfo'],$_GET['user_id']);
		 }else{
			$get_user_id='0';
		 }
			 if($get_user_id>=0){
			 unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		}
	  }else if(isset($_GET['user_id']) && $_GET['user_id']!=""){
		if(isset($_SESSION['usersinfo']) && $_SESSION['usersinfo']!=""){
			$get_user_id = $this->getusersnewTable()->editUpdate2($_SESSION['usersinfo'],$_GET['user_id']);
		}else{
			$get_user_id='0';
		}
		if($get_user_id>=0){
		 unset($_SESSION['usersinfo']);	
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser($_GET['user_id']);
			 $countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			 $getCountries=$countrytable->getallcountries();
			 return new ViewModel(array(
				'user_details' 	=> $getdetails,
				'countries' 	=> $getCountries,
			 ));
		}
	  }else{		
		$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
		 $getCountries=$countrytable->getallcountries();
		return new ViewModel(array(
			'countries' 	=> $getCountries,
		 ));
	  }
	}

	public function leftMenuAction(){
		return $result = new ViewModel();		
	}
	public function loginAction(){
	$request = $this->getRequest();
			$count='';
			$userDetailes='';
			if ($request->isPost()){
			        $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
					$checkLogin = $table->checkLogin($_POST,$authreg='');
					if($checkLogin!=''){					
						$user_session = new Container('user');
						$user_session->username=$checkLogin->company_name;
						$user_session->email=$checkLogin->email;
						$user_session->user_id=$checkLogin->user_id;
						$user_session->user_type=$checkLogin->user_type;
						$user_session->countryname=$checkLogin->usercountry;
						$user_session->country_id=$checkLogin->country;
						$user_session->cityname=$checkLogin->city;
						if($checkLogin->user_id>0){
							$countProducts = $this->getProductsnewTable()->getActivestatus($checkLogin->user_id);
							$user_session->countProducts=$countProducts;
							$countBidrecived = $this->getBidTable()->bidrecivedCount($checkLogin->user_id);
							$user_session->countBidrecived=$countBidrecived;
						}
						$result = new JsonModel(array(					
							'output' 		=> 'success',
							'user_type'		=>	$checkLogin->user_type,
							'user_id'       =>  $checkLogin->user_id,
							'success'		=>	true,
						));	
						
					}else{
						$result = new JsonModel(array(					
							'output' 		=> 'notsuccess',							
							'notsuccess'		=>	true,
						));	
					}
                 					
				return $result;
			}else{
				$viewModel = new ViewModel();
				$viewModel->setVariable('options', $this->getOptions());
				$viewModel->setTerminal(true);
				return $viewModel;
			}
	}
	public function logoutAction(){	
       unset($_SESSION['user']);
	   unset($_SESSION['usersinfo']);
	   unset($_SESSION['products']);
	   $result = new JsonModel(array(					
			'output' => 'success',
			'success'=>false,
		));
		return $result;
	}
	public function checkregemailAction(){	
		if(isset($_POST['email']) && $_POST['email']!=""){	
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$emailCount = $table->checkEmail($_POST['email']);
			if($emailCount!='0'){
				$result = new JsonModel(array(					
					'output' 	=> 'success',
				));	
			}else{
				$result = new JsonModel(array(					
					'output' 	=> 'notsuccess',
				));
			}
			return $result;			
		}
	}	
	
	public function checkadmincombinationsAction(){
		if(isset($_POST['combinations']) && $_POST['combinations']!=""){
			$hid_user_id = "";
			if(isset($_POST['hid_user_id']) && $_POST['hid_user_id']!=""){
				$hid_user_id = $_POST['hid_user_id'];
			}
			$combsStatus = "";
			$table = $this->getServiceLocator()->get('Users\Model\AdminCombinationsTable');
			$combRows = explode( "!",$_POST['combinations'] );
			for( $combRowId = 0; $combRowId < count($combRows); $combRowId++  )
			{
				$combsCount = $table->checkexistingCombs( $combRows[$combRowId],$hid_user_id );
				$combsStatus .= $combRows[$combRowId] . "-" . $combsCount . "!";
			}
			$result = new JsonModel(array(					
				'output' 	=> 'success',
				'combsStatus' 	=> $combsStatus,
			));	
			return $result;			
		}
	}	
	
	public function loadCountryIndustriesAction(){
		if(isset($_POST['countrytId']) && $_POST['countrytId']!=""){
			$industriesInfo = "";
			$table = $this->getServiceLocator()->get('Products\Model\CountryAndIndustryTable');
			
			$resultSet = $table->getCountryIndustries( $_POST['countrytId'] );
			
			foreach( $resultSet as $currIndustryRow )
			{
				$industriesInfo .= $currIndustryRow->industry_id . "/" . $currIndustryRow->industryname . "!";
			}
			$industriesInfo = substr( $industriesInfo,0,(strlen($industriesInfo)-1) );

			if( $industriesInfo != null )
			{
				$result = new JsonModel(array(					
					'output' 	=> 'success',
					'industriesInfo' 	=> $industriesInfo,
				));	
			}
			else
			{
				$result = new JsonModel(array(					
					'output' 	=> 'notsuccess',
				));
			}
			return $result;			
		}
	}	

	
	public function registerAction()
	{
		$request = $this->getRequest();
        if ($request->isPost()) {
			global $regSubject,$regMessage;	
			$password = $this->getUniqueCode('10');			
			$user_type=$this->getUserTable()->addUser($_POST,$password);	
			$userId=$user_type->user_id;
			$username=$user_type->username;
			$useremail=$user_type->email;			
			//Mailing				
				global $regSubject;				
				global $regMessage;
				$regMessage = str_replace("<FULLNAME>","$username", $regMessage);
				$regMessage = str_replace("<ACTIVATIONLINK>","http://" . $_SERVER['HTTP_HOST']."/users/reg-authentication?uid=".base64_encode($userId), $regMessage);	
				$to=$useremail;				
				include('public/PHPMailer_5.2.4/sendmail.php');				
				if(sendMail($to,$regSubject,$regMessage)){
					$result = new JsonModel(array(					
						'output' 		=> 'success',
						'success'		=>	true,
					));	
				}else{
					$result = new JsonModel(array(					
						'output' => 'not success',
						'not success'=>false,
					));	
				}
				return $result;
			}else{
				$table = $this->getServiceLocator()->get('Application\Model\countryTable');
				$getCountries=$table->getallcountries();
				$viewModel = new ViewModel();
				$viewModel->setVariable('countries', $getCountries);
				return $viewModel;
			}
    }
	public function regAuthenticationAction(){
		$userid=base64_decode($_GET['uid']);
		$userAuth=$this->getUserTable()->updateUserRegAuth($userid);
		$users=$this->getUserTable()->editUser($userid);
		$to=$users->email;
		$userName=$users->username;
		$this->userlogin($users);	
		if(isset($_SESSION['sessionUrl'])){
			return $this->redirect()->toUrl($_SESSION['sessionUrl']);
		}else{
			return $this->redirect()->toUrl('/application/index/index');
		}		
	}
	public function userlogin($users)
	{
		$userDetailes=array();
		$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
		$_POST['userEmail']=$users->email;
		$_POST['userPassword']=$users->password;
		$checkLogin = $table->checkLogin($_POST,$regauth='1');		
		$user_session = new Container('user');
		$user_session->username=$checkLogin->company_name;
		$user_session->email=$checkLogin->email;
		$user_session->user_id=$checkLogin->user_id;
		$user_session->user_type=$checkLogin->user_type;		
		$userDetailess = $table->getuserDetailes($checkLogin->user_id);
		foreach($userDetailess as $user){			
			$userDetailes=$user;}	
		return true;
	}
	public function getprofileAction(){	
		$user_id=base64_decode($_GET['id']);					
		if(isset($user_id)){							
			$table = $this->getServiceLocator()->get('Users\Model\UserTable');
			$user_info= $table->editUser($user_id);
			$countrytable = $this->getServiceLocator()->get('Application\Model\countryTable');
			$getCountries=$countrytable->getallcountries();
			$statetable = $this->getServiceLocator()->get('Application\Model\stateTable');
			$getStates=$statetable->getallstates();				
			return new ViewModel(array(
				'user_details' 	=> $user_info,
				'countries' 	=> $getCountries,
				'states' 	=> $getStates,
			));
		}
	}
	public function updateprofileAction(){
	    if(isset($_POST['hiduserid'])){	
			$userid=$this->getUserTable()->updateUser($_POST);
			$result = new JsonModel(array(					
						'output' => 'Update_success',
						'Update_success'=>true,
			));	
			return $this->redirect()->toUrl('/users/profile');
		}
	}
	public function getUserTable()
    {
        if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('Users\Model\UserTable');
        }
        return $this->userTable;
    }		
	public function getAdminCombinationsTable()
    {
        if (!$this->adminCombinationsTable) {
            $sm = $this->getServiceLocator();
            $this->adminCombinationsTable = $sm->get('Users\Model\AdminCombinationsTable');
        }
        return $this->adminCombinationsTable;
    }		
	public function getUserRegionsTable()
    {
        if (!$this->userRegionsTable) {
            $sm = $this->getServiceLocator();
            $this->userRegionsTable = $sm->get('Users\Model\UserRegionsTable');
        }
        return $this->userRegionsTable;
    }		
	public function checkPasswordAction(){	
		$pwdExit = $this->getusersnewTable()->getpassword($_POST['oldpasswrd'],$_POST['userId']);
		if($pwdExit>0){			
			$result = new JsonModel(array(					
				'output' => 'success',
			));			
		}else{
			$result = new JsonModel(array(					
				'output' => 'not success',
			));
		}
		return $result;		
	}	
	public function changePasswordAction()
	{
	    if(isset($_POST['cnfpwrd'])){
			$changepwd = $this->getusersnewTable()->changepwd($_POST['cnfpwrd'],$_POST['userId']);	
		}
		$view=new ViewModel(array(
		));
		return $view;		
	}
	
	public function forgetpasswordAction(){	
		$baseUrls = $this->getServiceLocator()->get('config');
        $baseUrlArr = $baseUrls['urls'];
        $baseUrl = $baseUrlArr['baseUrl'];
		if(isset($_POST['email']) && $_POST['email']!=""){		
			global $forgotPasswordSubject,$frogotPasswordMessage;	
			$userinfo=array();
			$mailinfo=array();
			$email=$_POST['email'];			
			$uniqueRecardemail = $this->getusersnewTable()->checkUniqueRecord($email);			
			if($uniqueRecardemail>0){				
				$arr=$this->getusersnewTable()->getMembersByMailId($email);	
				foreach($arr as $userinfo){}				 	
				$user_id=$userinfo->user_id;
				$username=$userinfo->company_name;				
				$token = $this->getUniqueCode('10');				
				$tokentime = date("Y-m-d H:i:s", time()) ;				
				$mailExit=$this->getForgetpasswordTable()->getmailfromfgtpwd($email);					
				foreach($mailExit as $mailinfo){}				
				try {	
					if(count($mailinfo)!=""){					
						$alreadyexitid=$mailinfo->forget_pwd_id;
						$insForgotPassword = $this->getForgetpasswordTable()->addForgetpwd($alreadyexitid,$user_id,$email,$token);
					}else{						
						$alreadyexitid="";
						$insForgotPassword = $this->getForgetpasswordTable()->addForgetpwd($alreadyexitid,$user_id,$email,$token);	
					}
				}catch (\Exception $ex) {
					$insForgotPassword="";
				}
				//Mailing				
				global $forgotPasswordSubject;				
				global $frogotPasswordMessage;
				$frogotPasswordMessage = str_replace("<FULLNAME>","$username", $frogotPasswordMessage);
				$frogotPasswordMessage = str_replace("<PASSWORDLINK>",$baseUrl."/newpassword?token=".$token, $frogotPasswordMessage);	
				$to=$email;				
				include('public/PHPMailer_5.2.4/sendmail.php');				
				if(sendMail($to,$forgotPasswordSubject,$frogotPasswordMessage)){
					$result = new JsonModel(array(					
						'output' => 'success',
						'success'=>true,
						'type'=>'user',
					));	
				}else{
					$result = new JsonModel(array(					
						'output' => 'not success',
						'not success'=>false,
					));	
				}
				return $result;
			}
		}	
	}	
	public function getUniqueCode($length = "")
	{
		$code = md5(uniqid(rand(), true));
		if ($length != "")
		return substr($code, 0, $length);
		else
		return $code;
	}
	public function getForgetpasswordTable()
    {
        if (!$this->forgetpasswordTable) {
            $sm = $this->getServiceLocator();
            $this->forgetpasswordTable = $sm->get('users\Model\ForgetpasswordTable');			
        }
        return $this->forgetpasswordTable;
    }
	public function updatepasswordAction(){
		$token=$_GET['token'];
		$tokeninfo=array();
		$exitToke=0;
		$tokenExit=$this->getForgetpasswordTable()->gettoken($token);		
		if(count($tokenExit)!=0){		
			foreach($tokenExit as $tokeninfo){}				
				if($tokeninfo->forget_pwd_id > 0){
					$exitToken=1;
				}else{
					$exitToken=0;
				}
		}else{
			$exitToken=0;
		}
		$viewModel = new ViewModel(
			array('existtoken'=>$exitToken)
		);	
		return $viewModel;			
	}
	public function setnepasswordAction(){
		if(isset($_POST['token']) && $_POST['token']!=""){
			$token=$_POST['token'];		
			$tokeninfo=array();
			$tokenExit=$this->getForgetpasswordTable()->gettoken($token);
			foreach($tokenExit as $tokeninfo){}
			if($tokeninfo->user_id>0){
				$changepwd = $this->getusersnewTable()->changepwd($_POST['cnfpwrd'],$tokeninfo->user_id);						
				if($changepwd>=0){
					$userAuth=$this->getusersnewTable()->updateUserRegAuth($tokeninfo->user_id);
					$users=$this->getusersnewTable()->editUser($tokeninfo->user_id);
					$this->userlogin($users);
					$deletetokenid=$this->getForgetpasswordTable()->deletetoken($tokeninfo->forget_pwd_id);
					$result = new JsonModel(array(					
						'output' => 'success',
						'success'=>false,
					));			
				}else{
					$result = new JsonModel(array(					
						'output' => 'not success',
						'not success'=>false,
					));
				}				
			}else{
				$result = new JsonModel(array(					
					'output' => 'not success',
					'not success'=>false,
				));	
			}
			return $result;	
		}
	}	
	public function getCountryTable()
    {
        if (!$this->countryTable) {
            $sm = $this->getServiceLocator();
            $this->countryTable = $sm->get('Application\Model\CountryTable');			
        }
        return $this->countryTable;
    }
	public function getStateTable()
    {
        if (!$this->stateTable) {
            $sm = $this->getServiceLocator();
            $this->stateTable = $sm->get('Application\Model\StateTable');			
        }
        return $this->stateTable;
    }	
	public function getStatesAction(){	
		$html="";
		if(isset($_POST['countryid'])){
			$table = $this->getServiceLocator()->get('Application\Model\StateTable');
			$states=$table->getBasedcountry($_POST['countryid']);			
			$html.='<option value="">State</option>';
			foreach($states as $statename){
				$html.='<option value="'.$statename->state_id.'">'.$statename->state_name.'</option>';
			}			
			$result = new JsonModel(array(					
				'output' => 'success',
				'success'=>true,
				'statenames'=>$html,
				));
			return $result;
		}
	}
	public function usermenuAction(){
	
	}
	public function getusersnewTable()
	{
		if (!$this->usersnewTable) {
			$sm = $this->getServiceLocator();
			$this->usersnewTable = $sm->get('Users\Model\UsersnewTable');
		}
		return $this->usersnewTable;
	}
	public function adminAllusersListAction()
	{	
	}
	public function alladminusersAjaxAction()
	{
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basepath = $baseUrlArr['basePath'];
		if(isset($_GET['userType']) && $_GET['userType']!=''){
			$userdata = $this->getusersnewTable()->getallAdminUsers();
			$data = array();$i=0;$admin='';
				if(isset($userdata) && $userdata!=''){
					foreach($userdata as $key=>$value){
						$id=$value->user_id;
						if($value->u_status=='0'){
							$status='Pending';
						}else if($value->u_status=='1'){
							$status='Active';
						}else{
							$status='InActive';
						}
						$data[$i]['sno']=$key+1;
						$data[$i]['companyname']= $value->company_name;
						$data[$i]['status'] =$status;
						$data[$i]['email'] =$value->email;
						$data[$i]['phone'] =$value->phone;
						$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/adduser?user_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onclick="deleteUser('.$id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
						$i++;
					}
				}
				$data['aaData'] = $data;
				print_r(json_encode($data['aaData']));exit;
		}
	}
	public function usersListAction()
	{	
	}
	public function adminUsersAction()
	{
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basepath = $baseUrlArr['basePath'];
		if(isset($_GET['user_id']) && $_GET['user_id']!=''){
			$userdata = $this->getusersnewTable()->getadminUsers($_GET['user_id'],$user_user_id='',$check='');
			$data = array();$i=0;$admin='';
				if(isset($userdata) && $userdata!=''){
					foreach($userdata as $key=>$value){
						$id=$value->user_id;
						if($value->u_status=='0'){
							$status='Pending';
						}else if($value->u_status=='1'){
							$status='Active';
						}else{
							$status='InActive';
						}
						$data[$i]['sno']=$key+1;
						$data[$i]['companyname']= $value->company_name;
						$data[$i]['status'] =$status;
						$data[$i]['email'] =$value->email;
						$data[$i]['phone'] =$value->phone;
						$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/adduser?user_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>';
						$i++;
					}
				}
				$data['aaData'] = $data;
				print_r(json_encode($data['aaData']));exit;
		}
	}
	public function allusersListAction()
	{	
	}
	public function allusersAjaxAction()
	{
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basepath = $baseUrlArr['basePath'];
		if(isset($_GET['userType']) && $_GET['userType']!=''){
			$userdata = $this->getusersnewTable()->getdataUsers($_GET['userType']);
			$data = array();$i=0;$admin='';
				if(isset($userdata) && $userdata!=''){
					foreach($userdata as $value){
						$id=$value->user_id;
						if($value->u_status=='0'){
							$status='Pending';
						}else if($value->u_status=='1'){
							$status='Active';
						}else{
							$status='InActive';
						}
						$data[$i]['sno']=$i+1;
						$data[$i]['companyname']= $value->company_name;
						$data[$i]['status'] =$status;
						$data[$i]['email'] =$value->email;
						$data[$i]['phone'] =$value->phone;						
						if($value->user_type=='admin'){
							$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/addadmin?user_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>
							&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onclick="deleteUser('.$id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>
							&nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/users-list?user_id='.$id.'">Users List</a>';
						}else{
							$data[$i]['action'] ='<a style="color: #000000;font-size: 15px;" href="'.$baseUrl.'/users/adduser?user_id='.$id.'"><img src="'.$basepath.'/images/edit.png" title="Edit"></a>
						   &nbsp;/&nbsp;<a style="color: #000000;font-size: 15px;" href="javascript:void(0);" onclick="deleteUser('.$id.');"><img src="'.$basepath.'/images/delete.png" title="Delete"></a>';
						}
						$i++;
					}
				}
				$data['aaData'] = $data;
				print_r(json_encode($data['aaData']));exit;
		}
	}
	
	public function addCountryAction(){
		if(isset($_POST['country_name']) && $_POST['country_name']!=""){
			$add_county=$this->getCountryTable()->addCountry($_POST);
			if($add_county>=0){
				$result = new JsonModel(array(					
					'output' => 'success',
					'success'=>true,					
				));
			}else{
				$result = new JsonModel(array(					
					'output' => 'notsuccess',
					'success'=>true,					
				));
			}
			return $result;
		}else if(isset($_GET['country_id']) && $_GET['country_id']!=""){
			$getcontry=$this->getCountryTable()->getcountry($_GET['country_id']);
			return new ViewModel(array(
				'getcontryInfo' 	=> $getcontry,				
			));
		}
	}
	public function checkcountryAction(){
		if(isset($_POST['country_name']) && $_POST['country_name']!=""){
			$status_conutry=$this->getCountryTable()->checkoncountry(strtoupper($_POST['country_name']));			
			if($status_conutry!=""){
				$result = new JsonModel(array(					
					'output' => 'success',
					'success'=>true,
					'country_name'=>$status_conutry->country_name,
				));
			}else{
				$result = new JsonModel(array(					
					'output' => 'notsuccess',
					'success'=>true,					
				));
			}
			return $result;
		}
	}
	public function deleteusersAction(){
		if(isset($_GET['user_id']) && $_GET['user_id']!=""){
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$givenUserdetails = $table->editUser( $_GET['user_id'] );
			
			if( $givenUserdetails->user_type == "admin" )
			{
				$deleteAdminStatus=$this->getAdminCombinationsTable()->deleteCombinations( $_GET['user_id'] );
			}
			if( $givenUserdetails->user_type == "user" )
			{
				$deleteRegionsStatus=$this->getUserRegionsTable()->deleteRegions( $_GET['user_id'] );
			}
		
		   $userdelete=$this->getUsersnewTable()->getUserdelete($_GET['user_id']);
		   $changedStatus = $this->getProductsnewTable()->changeStatus($_GET['user_id']);
		}
		
		if( isset($_SESSION['usersinfo']['type']) && $_SESSION['usersinfo']['type'] == 'superadmin' ){
			return $this->redirect()->toUrl('allusers-list');
		}else if( isset($_SESSION['usersinfo']['type']) && $_SESSION['usersinfo']['type'] == 'admin' ){
			return $this->redirect()->toUrl('admin_allusers-list');
		}else if( isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type'] == 'superadmin' ){
			return $this->redirect()->toUrl('allusers-list');
		}else if( isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type'] == 'admin' ){
			return $this->redirect()->toUrl('admin_allusers-list');
		}
	}
	public function getProductsnewTable()
    {
        if (!$this->productsnewTable) {				
            $sm = $this->getServiceLocator();
            $this->productsnewTable = $sm->get('Products\Model\ProductsnewTable');			
        }
        return $this->productsnewTable;
    }
	public function getBidTable()
    {
        if (!$this->bidTable) {
            $sm = $this->getServiceLocator();
            $this->bidTable = $sm->get('Products\Model\BidTable');
        }
        return $this->bidTable;
    }	
	public function getCountiresIndustriesAction(){
		if(isset($_POST['admin_user_id']) && $_POST['admin_user_id']!=""){
			$countries='';
			$industires='';
			$admin_user_id=explode("/",$_POST['admin_user_id']);
			$getallindustries=$this->getAdminCombinationsTable()->getindustries( $admin_user_id[0],'industry' );
			$getRegions=$this->getAdminCombinationsTable()->getindustries( $admin_user_id[0],'country' );
			$countries.='<option value="">Select a country</option>';
			$industires.='<option value="">Select a industry</option>';
			foreach($getallindustries as $industries){
				if(isset($_SESSION['usersinfo']['industry']) && $_SESSION['usersinfo']['industry']==$industries->industry_id){
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'" selected="selected">'.$industries->industryNicename.'</option>';
				}else if(isset($_POST['industry_id']) && $_POST['industry_id']==$industries->industry_id){
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'" selected="selected">'.$industries->industryNicename.'</option>';
				}else{
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'">'.$industries->industryNicename.'</option>';
				}
			}
			foreach($getRegions as $regions){
				if(isset($_SESSION['usersinfo']['country']) && $_SESSION['usersinfo']['country']==$regions->country_id){
					$countries.='<option value="'.$regions->country_id.'/'.$regions->countryNicename.'">'.$regions->countryNicename.'</option>';
				}else if(isset($_POST['industry_id']) && $_POST['industry_id']==$regions->country_id){
					$countries.='<option value="'.$regions->country_id.'/'.$regions->countryNicename.'" selected="selected">'.$regions->countryNicename.'</option>';
				}else{
					$countries.='<option value="'.$regions->country_id.'/'.$regions->countryNicename.'">'.$regions->countryNicename.'</option>';
				}
			}			
			$result = new JsonModel(array(					
				'output' => 'success',
				'countries'=>$countries,
				'industires'=>$industires,
			));			
		}else{
			$countries.='<option value="">Select a country</option>';
			$industires.='<option value="">Select a industry</option>';
			$result = new JsonModel(array(					
				'output' => 'notsuccess',
				'countries'=>$countries,
				'industires'=>$industires,
			));
		}
		return $result;
	}

	public function getAdminCountryIndustriesAction(){
		if(isset($_POST['admin_user_id']) && $_POST['admin_user_id']!=""){
			$industires='';
			$admin_user_id=$_POST['admin_user_id'];
			$country_id=explode("-",$_POST['country_id']);
			if( ! array_key_exists( 1,$country_id) )
			{
				$country_id=explode("/",$_POST['country_id']);
			}
			$industry_id=$_POST['industry_id'];
			$table = $this->getServiceLocator()->get('Products\Model\CountryAndIndustryTable');
			$getallindustries=$this->getAdminCombinationsTable()->getAdminIndustries( $admin_user_id,$country_id[0] );
			$industires.='<option value="">Select a industry</option>';
			foreach($getallindustries as $industries){
				if(isset($_SESSION['usersinfo']['industry']) && $_SESSION['usersinfo']['industry']==$industries->industry_id){
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'" selected="selected">'.$industries->industryNicename.'</option>';
				}else if(isset($_POST['industry_id']) && $_POST['industry_id']==$industries->industry_id){
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'" selected="selected">'.$industries->industryNicename.'</option>';
				}else{
					$industires.='<option value="'.$industries->industry_id.'/'.$industries->industryNicename.'">'.$industries->industryNicename.'</option>';
				}
			}
			$result = new JsonModel(array(					
				'output' => 'success',
				'industires'=>$industires,
			));			
		}else{
			$industires.='<option value="">Select a industry</option>';
			$result = new JsonModel(array(					
				'output' => 'notsuccess',
				'industires'=>$industires,
			));
		}
		return $result;
	}
	
	public function myArraysCombine( $key,$val )
	{
		return array( $key=>$val );
	}	
	
	public function transferUsersAction(){
		$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
		if(isset($_POST['adminId']) && $_POST['adminId']!=''){
			$userCount='';
			//$allCombinations = $table->getallCombinations($_POST['adminId']);
			$allCombinations = $this->getAdminCombinationsTable()->getallcombinations($_POST['adminId']);
			$allCombinations->buffer();				
			$result = new ViewModel(array(					
				'allCombinations'=>$allCombinations,
			));
			$result->setTemplate("users/users/ajax-combinations.phtml");;
		    $result->setTerminal(true);
		    return $result;
		}else if(isset($_POST['type'])){
			$fromAdminId=$_POST['fromAdminId'];
			if($_POST['type']=='old'){
				$toAdminId=$_POST['toAdminId'];
			}else{
				$companyname=$_POST['companyname'];
				$emailcheck=$_POST['emailcheck'];
				$pnumber=$_POST['pnumber'];
				$password = $this->getUniqueCode('10');
				$toAdminId=$table->addNewAdmin($companyname,$emailcheck,$pnumber,$password);
			}
			$ids=explode('&&',rtrim($_POST['ids'],'&&'));
			foreach($ids as $combination){ 
				$com=explode(',',$combination);
				$countryId=$com[0];
				$industryId=$com[1];
				$updateAdmin = $table->updateAdminId($countryId,$industryId,$fromAdminId,$toAdminId);
				$updateAdminCombination = $this->getAdminCombinationsTable()->updateAdmin($countryId,$industryId,$toAdminId);
				if(count($updateAdmin)>0){
					foreach($updateAdmin as $userIds){					
						$updateproductadmin = $this->getProductsnewTable()->updateuserproducts_admin($toAdminId,$userIds->user_id);
					}
				}
			}
			$allCombinations = $this->getAdminCombinationsTable()->getallcombinations($fromAdminId);
			$result = new ViewModel(array(					
				'allCombinations'=>$allCombinations->buffer(),
			));
			$result->setTemplate("users/users/ajax-combinations.phtml");;
			$result->setTerminal(true);
			return $result;
		}else{
			$getalladmins = $table->getalladmins();
			return $result = new ViewModel(array(					
				'allAdmins'=>$getalladmins->buffer(),
			));	
		}
	}
	public function getCombinationUsersAction(){
		if(isset($_POST['combination_id']) && $_POST['combination_id']!=""){
			$combinationDetails = $this->getAdminCombinationsTable()->combinationDetails($_POST['combination_id']);
			$industryId=$combinationDetails->industry_id;
			$countryId=$combinationDetails->country_id;
			$table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$getUsers = $table->getcombination_users($industryId,$countryId,$_POST['admin_user_id']);
			$result = new ViewModel(array(					
				'combinationsusers'=>$getUsers->buffer(),
			));
			$result->setTemplate("users/users/combination-users.phtml");;
		    $result->setTerminal(true);
		    return $result;
		}
	}
}		

	
